// 这份代码专门处理逻辑层的操作
const taskDao = require('../dao/taskDao');
const powerDao = require('../dao/powerDao');

const taskService = {
    /*
     * 添加任务
     */
    async addTask(uid, name, description, count) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '添加任务');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            result = await taskDao.addTask(uid, name, description, count);
            if (result.insertId) {
                result = { success: true, msg: '添加任务成功' };
            } else {
                result = { success: false, msg: '添加任务失败' };
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 删除任务
     */
    async deleteTask(uid, taskId) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '删除任务');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            result = await taskDao.deleteTask(uid, taskId);
            result = { success: true, msg: '删除任务成功' };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 设置任务已完成
     */
    async setTaskComplete(uid, taskId) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '打卡任务');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            result = await taskDao.setTaskComplete(uid, taskId);
            if (result.insertId) {
                result = { success: true, msg: '打卡任务成功' };
            } else {
                result = { success: false, msg: '今日已打卡该任务' };
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 获取任务列表
     */
    async getTaskList(uid) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看任务');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            result = await taskDao.getTaskList(uid);
            result = { success: true, msg: '获取所有任务成功', data: { taskList: result } };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    // 获取所有任务打卡记录
    async getAllTaskCheckin(uid, page) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '打卡管理');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            result = await taskDao.getAllTaskCheckin(uid, page);
            result = { success: true, msg: '获取所有任务成功', data: result };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    }
}

module.exports = taskService;